package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.model.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.product.client.ProductFeignClient;
import com.atguigu.gmall.product.model.SkuInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 购物车控制器
 */
@RestController
@RequestMapping("/api/cart")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private ProductFeignClient productFeignClient;

    /**
     * 将商品加入购物车
     * @param request
     * @param skuId 商品skuId
     * @param skuNum 商品数量
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(HttpServletRequest request, @PathVariable("skuId") Long skuId,@PathVariable("skuNum") Integer skuNum){
        //获取用户真实id或临时id
        //1.优先使用登录后的真实id
        String userId = AuthContextHolder.getUserId(request);

        //2.用户未登录,在获取临时用户id
        if(StringUtils.isBlank(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        //3.调用业务层将商品加入购物车
        cartService.addToCart(userId,skuId,skuNum);
        return Result.ok();
    }

    /**
     * 查询当前用户购物车列表(登录用户,临时用户)
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result getCartList(HttpServletRequest request){
        //获取用户id(真实id或临时id)
        //1.优先使用登录后用户id
        String userId = AuthContextHolder.getUserId(request);
        //2.用户未登录,获取用户临时id
        String userTempId = AuthContextHolder.getUserTempId(request);
        //3.调用业务层获取购物车列表
        List<CartInfo> cartInfoList = cartService.getCartList(userId,userTempId);
        return Result.ok(cartInfoList);
    }

    /**
     * 修改购物车商品选中状态
     * @param request
     * @param skuId
     * @param isChecked
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result updateCheck(HttpServletRequest request,@PathVariable("skuId") Long skuId,@PathVariable("isChecked") Integer isChecked){
        //获取临时用户id,登录用户id
        //1.优先使用登录后用户id
        String userId = AuthContextHolder.getUserId(request);
        //2.用户未登录在获取临时用户id
        if(StringUtils.isBlank(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.updateCheck(userId,skuId,isChecked);
        return Result.ok();
    }

    /**
     * 删除购物车中指定商品
     * @param request
     * @param skuId
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(HttpServletRequest request,@PathVariable("skuId") Long skuId){
        //获取用户id(真实用户id或临时用户id)
        //1.优先使用登录后用户id
        String userId = AuthContextHolder.getUserId(request);
        //2.用户未登录id在获取临时用户id
        if(StringUtils.isBlank(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.deleteCart(userId,skuId);
        return Result.ok();
    }

    /**
     * 加入购物车成功页面渲染
     * @param model
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/addCart.html")
    public String addCartSuccess(Model model, @RequestParam("skuId") Long skuId, @RequestParam("skuNum") Integer skuNum){
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        model.addAttribute("skuInfo", skuInfo);
        model.addAttribute("skuNum", skuNum);
        return "cart/addCart";
    }

    /**
     * 渲染购物车列表页面
     * @return
     */
    @GetMapping("/cart.html")
    public String cartHtml(){
        return "cart/index";
    }
}